package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("CrawlerfilestandardcolumnService")
@Transactional

public class CrawlerfilestandardcolumnServiceImpl extends CmiEmaService implements CrawlerfilestandardcolumnService {
	public CrawlerfilestandardcolumnServiceImpl() {
	}

	@Transactional
	public Crawlerfilestandardcolumn saveCrawlerfilestandardcolumn(
			Crawlerfilestandardcolumn crawlerfilestandardcolumn) {
		Set<Crawlerfilecolumn> existingCrawlerfilecolumnsForCrawlerfilecolumn = new HashSet<Crawlerfilecolumn>();
		Set<Crawlerfilestandardetail> existingCrawlerfilestandardetailsForCrawlerfilestandardcolumn = new HashSet<Crawlerfilestandardetail>();

		Set<Crawlerfilecolumn> selectedCrawlerfilecolumnsForCrawlerfilecolumn = crawlerfilestandardcolumn
				.getCrawlerfilecolumnsForCrawlerfilestandardcolumn();
		Set<Crawlerfilestandardetail> selectedCrawlerfilestandardetailsForCrawlerfilestandardcolumn = crawlerfilestandardcolumn
				.getCrawlerfilestandardetailsForCrawlerfilestandardcolumn();

		if (crawlerfilestandardcolumn.getId() == null) {

			Crawlerfilestandardcolumn existingCrawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
					.findCrawlerfilestandardcolumnByPhysicalPrimaryKey(crawlerfilestandardcolumn.getName());

			if (isEquals(crawlerfilestandardcolumn, existingCrawlerfilestandardcolumn)) {
				return existingCrawlerfilestandardcolumn;
			}

			crawlerfilestandardcolumn = crawlerfilestandardcolumnDAO.store(crawlerfilestandardcolumn);
			crawlerfilestandardcolumnDAO.flush();

			for (Crawlerfilecolumn relatedCrawlerfilecolumn : selectedCrawlerfilecolumnsForCrawlerfilecolumn) {
				relatedCrawlerfilecolumn.getCrawlerfilestandardcolumnsForCrawlerfilecolumn()
						.add(crawlerfilestandardcolumn);
				crawlerfilecolumnDAO.store(relatedCrawlerfilecolumn);
			}
			for (Crawlerfilestandardetail relatedCrawlerfilestandardetail : selectedCrawlerfilestandardetailsForCrawlerfilestandardcolumn) {
				relatedCrawlerfilestandardetail.setCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
				crawlerfilestandardetailDAO.store(relatedCrawlerfilestandardetail);
			}
		} else {
			Crawlerfilestandardcolumn existingCrawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
					.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumn.getId());

			existingCrawlerfilecolumnsForCrawlerfilecolumn = existingCrawlerfilestandardcolumn
					.getCrawlerfilecolumnsForCrawlerfilestandardcolumn();
			existingCrawlerfilestandardetailsForCrawlerfilestandardcolumn = existingCrawlerfilestandardcolumn
					.getCrawlerfilestandardetailsForCrawlerfilestandardcolumn();

			Set<Crawlerfilecolumn> prepareDeleteCrawlerfilecolumnsForCrawlerfilecolumn = new HashSet<Crawlerfilecolumn>();
			Set<Crawlerfilecolumn> prepareAddCrawlerfilecolumnsForCrawlerfilecolumn = new HashSet<Crawlerfilecolumn>();

			prepareDeleteCrawlerfilecolumnsForCrawlerfilecolumn.addAll(selectedCrawlerfilecolumnsForCrawlerfilecolumn);
			prepareDeleteCrawlerfilecolumnsForCrawlerfilecolumn.addAll(existingCrawlerfilecolumnsForCrawlerfilecolumn);

			prepareAddCrawlerfilecolumnsForCrawlerfilecolumn.addAll(selectedCrawlerfilecolumnsForCrawlerfilecolumn);
			prepareAddCrawlerfilecolumnsForCrawlerfilecolumn.addAll(existingCrawlerfilecolumnsForCrawlerfilecolumn);

			prepareDeleteCrawlerfilecolumnsForCrawlerfilecolumn
					.removeAll(selectedCrawlerfilecolumnsForCrawlerfilecolumn);
			prepareAddCrawlerfilecolumnsForCrawlerfilecolumn.removeAll(existingCrawlerfilecolumnsForCrawlerfilecolumn);

			for (Crawlerfilecolumn relatedCrawlerfilecolumn : prepareAddCrawlerfilecolumnsForCrawlerfilecolumn) {

				relatedCrawlerfilecolumn.getCrawlerfilestandardcolumnsForCrawlerfilecolumn()
						.add(existingCrawlerfilestandardcolumn);
				crawlerfilecolumnDAO.store(relatedCrawlerfilecolumn);
			}

			for (Crawlerfilecolumn relatedCrawlerfilecolumn : prepareDeleteCrawlerfilecolumnsForCrawlerfilecolumn) {

				relatedCrawlerfilecolumn.getCrawlerfilestandardcolumnsForCrawlerfilecolumn()
						.remove(existingCrawlerfilestandardcolumn);
				crawlerfilecolumnDAO.store(relatedCrawlerfilecolumn);
			}
			Set<Crawlerfilestandardetail> prepareDeleteCrawlerfilestandardetailsForCrawlerfilestandardcolumn = new HashSet<Crawlerfilestandardetail>();
			Set<Crawlerfilestandardetail> prepareAddCrawlerfilestandardetailsForCrawlerfilestandardcolumn = new HashSet<Crawlerfilestandardetail>();

			prepareDeleteCrawlerfilestandardetailsForCrawlerfilestandardcolumn
					.addAll(selectedCrawlerfilestandardetailsForCrawlerfilestandardcolumn);
			prepareDeleteCrawlerfilestandardetailsForCrawlerfilestandardcolumn
					.addAll(existingCrawlerfilestandardetailsForCrawlerfilestandardcolumn);

			prepareAddCrawlerfilestandardetailsForCrawlerfilestandardcolumn
					.addAll(selectedCrawlerfilestandardetailsForCrawlerfilestandardcolumn);
			prepareAddCrawlerfilestandardetailsForCrawlerfilestandardcolumn
					.addAll(existingCrawlerfilestandardetailsForCrawlerfilestandardcolumn);

			prepareDeleteCrawlerfilestandardetailsForCrawlerfilestandardcolumn
					.removeAll(selectedCrawlerfilestandardetailsForCrawlerfilestandardcolumn);
			prepareAddCrawlerfilestandardetailsForCrawlerfilestandardcolumn
					.removeAll(existingCrawlerfilestandardetailsForCrawlerfilestandardcolumn);

			for (Crawlerfilestandardetail relatedCrawlerfilestandardetail : prepareAddCrawlerfilestandardetailsForCrawlerfilestandardcolumn) {
				relatedCrawlerfilestandardetail.setCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
				crawlerfilestandardetailDAO.store(relatedCrawlerfilestandardetail);
			}

			for (Crawlerfilestandardetail relatedCrawlerfilestandardetail : prepareDeleteCrawlerfilestandardetailsForCrawlerfilestandardcolumn) {
				relatedCrawlerfilestandardetail.setCrawlerfilestandardcolumn(null);
				crawlerfilestandardetailDAO.store(relatedCrawlerfilestandardetail);
			}
			crawlerfilestandardcolumn = crawlerfilestandardcolumnDAO.store(crawlerfilestandardcolumn);
			crawlerfilestandardcolumnDAO.flush();
		}

		crawlerfilecolumnDAO.flush();
		crawlerfilestandardetailDAO.flush();
		return crawlerfilestandardcolumn;
	}

	public void deleteCrawlerfilestandardcolumn(Crawlerfilestandardcolumn crawlerfilestandardcolumn) {
		crawlerfilestandardcolumnDAO.remove(crawlerfilestandardcolumn);
		crawlerfilestandardcolumnDAO.flush();
	}

	@Transactional
	public void deleteCrawlerfilestandardcolumns(Set<Integer> deleteCrawlerfilestandardcolumnsId) {
		Query query = crawlerfilestandardcolumnDAO.createNamedQuery("deleteCrawlerfilestandardcolumnsById", -1, -1);
		query.setParameter(1, deleteCrawlerfilestandardcolumnsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> loadCrawlerfilestandardcolumns() {
		return crawlerfilestandardcolumnDAO.findAllCrawlerfilestandardcolumns();

	}

	@Transactional
	public Set<Crawlerfilestandardcolumn> loadReferenceCrawlerfilestandardcolumns(
			Set<Crawlerfilestandardcolumn> crawlerfilestandardcolumns) {
		Iterator<Crawlerfilestandardcolumn> crawlerfilestandardcolumnsIterator = crawlerfilestandardcolumns.iterator();
		Set<Crawlerfilestandardcolumn> referencedCrawlerfilestandardcolumns = new HashSet<>();
		while (crawlerfilestandardcolumnsIterator.hasNext()) {
			Crawlerfilestandardcolumn currentCrawlerfilestandardcolumn = crawlerfilestandardcolumnsIterator.next();

			if (!currentCrawlerfilestandardcolumn.getCrawlerfilecolumnsForCrawlerfilestandardcolumn().isEmpty()) {
				referencedCrawlerfilestandardcolumns.add(currentCrawlerfilestandardcolumn);
				continue;
			}

			if (!currentCrawlerfilestandardcolumn.getCrawlerfilestandardetailsForCrawlerfilestandardcolumn()
					.isEmpty()) {
				referencedCrawlerfilestandardcolumns.add(currentCrawlerfilestandardcolumn);
				continue;
			}
		}
		return referencedCrawlerfilestandardcolumns;
	}

	public Set<Crawlerfilestandardcolumn> loadCrawlerfilestandardcolumnsByIdSet(
			Set<Integer> crawlerfilestandardcolumnIdSet) {

		Iterator<Integer> crawlerfilestandardcolumnIdIterator = crawlerfilestandardcolumnIdSet.iterator();
		Set<Crawlerfilestandardcolumn> crawlerfilestandardcolumnSet = new HashSet<>();
		while (crawlerfilestandardcolumnIdIterator.hasNext()) {
			Integer id = crawlerfilestandardcolumnIdIterator.next();
			Crawlerfilestandardcolumn currentCrawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
					.findCrawlerfilestandardcolumnById(id);
			crawlerfilestandardcolumnSet.add(currentCrawlerfilestandardcolumn);
		}
		return crawlerfilestandardcolumnSet;
	}

	@Transactional
	public List<Crawlerfilestandardcolumn> findAllCrawlerfilestandardcolumns(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Crawlerfilestandardcolumn>(
				crawlerfilestandardcolumnDAO.findAllCrawlerfilestandardcolumns(startResult, maxRows));
	}

	@Transactional
	public Crawlerfilestandardcolumn findCrawlerfilestandardcolumnByPrimaryKey(Integer id) {
		return crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByPrimaryKey(id);
	}

	@Transactional
	public Integer countCrawlerfilestandardcolumns() {
		return ((Long) crawlerfilestandardcolumnDAO
				.createQuerySingleResult("select count(o) from Crawlerfilestandardcolumn o").getSingleResult())
						.intValue();
	}

	@Transactional
	public Integer countCrawlerfilestandardcolumns(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Crawlerfilestandardcolumn.class);
		return ((Long) crawlerfilestandardcolumnDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countCrawlerfilestandardcolumnsByName(String queryName) {
		Query query = crawlerfilestandardcolumnDAO.createNamedQuery("countCrawlerfilestandardcolumnsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerfilestandardcolumnsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilestandardcolumn.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadCrawlerfilestandardcolumnObjectReferencedSet(
			Crawlerfilestandardcolumn crawlerfilestandardcolumn) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		Set<Integer> crawlerfilecolumnsIds = new HashSet<Integer>();
		for (Crawlerfilecolumn crawlerfilecolumn : crawlerfilestandardcolumn
				.getCrawlerfilecolumnsForCrawlerfilestandardcolumn()) {
			crawlerfilecolumnsIds.add(crawlerfilecolumn.getId());
		}
		objectReferencedSetMap.put("crawlerfilecolumnsIds", crawlerfilecolumnsIds);
		Set<Integer> crawlerfilestandardetailsIds = new HashSet<Integer>();
		for (Crawlerfilestandardetail obj : crawlerfilestandardcolumn
				.getCrawlerfilestandardetailsForCrawlerfilestandardcolumn()) {

			crawlerfilestandardetailsIds.add(obj.getId());
		}
		objectReferencedSetMap.put("crawlerfilestandardetailsIds", crawlerfilestandardetailsIds);

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadCrawlerfilestandardcolumnClassReferencedSet(
			Crawlerfilestandardcolumn crawlerfilestandardcolumn) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("crawlerfilecolumns", crawlerfilecolumnDAO.findAllCrawlerfilecolumns());
		classReferencedSetMap.put("crawlerfilestandardetails",
				crawlerfilestandardetailDAO.findAllCrawlerfilestandardetails());

		return classReferencedSetMap;
	}

	public boolean isEquals(Crawlerfilestandardcolumn crawlerfilestandardcolumn, Object obj) {
		if (obj == crawlerfilestandardcolumn)
			return true;
		if (!(obj instanceof Crawlerfilestandardcolumn))
			return false;
		Crawlerfilestandardcolumn comparedCrawlerfilestandardcolumn = (Crawlerfilestandardcolumn) obj;
		if (!CheckPropertyEqual(crawlerfilestandardcolumn.getName(), comparedCrawlerfilestandardcolumn.getName())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

